// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "user_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "用户表id，和uni-id-users保持一致",
    "label": "用户表id，和uni-id-users保持一致"
  },
  "mobile": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "手机号码",
    "label": "手机号码"
  },
  "name": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "姓名",
    "label": "姓名"
  },
  "avatar": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "骑手头像",
    "label": "骑手头像"
  },
  "id_card": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "身份证号",
    "label": "身份证号"
  },
  "is_online": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "在线状态，false：离线，true：在线",
    "defaultValue": true,
    "label": "在线状态，false：离线，true：在线"
  },
  "order_number": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "进行中订单数量",
    "defaultValue": 0,
    "label": "进行中订单数量"
  },
  "status": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "状态，false：禁用，true:正常",
    "defaultValue": true,
    "label": "状态，false：禁用，true:正常"
  },
  "order_total_number": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "总订单量",
    "defaultValue": 0,
    "label": "总订单量"
  },
  "money": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "余额，每月提现后会自动扣除",
    "defaultValue": 0,
    "label": "余额，每月提现后会自动扣除"
  },
  "create_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "创建时间"
  },
  "location": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "地理位置",
    "label": "地理位置"
  },
  "examine_status": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "审核状态",
    "label": "审核状态"
  },
  "photo": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "证件照",
    "label": "证件照"
  }
}

const enumConverter = {}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
